package providev.learn.imagina.tema1;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class tema1_1 extends Activity {
    /** Called when the activity is first created. */
	
	final ArrayList<String> todoItems = new ArrayList<String> ();
    ArrayAdapter<String> aa;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        ListView lstTodo = (ListView) findViewById(R.id.lstTodo);
        
        registerForContextMenu(lstTodo);

        final EditText txtTarea = (EditText) findViewById(R.id.txtTarea);

        aa = new ArrayAdapter<String>(this,

                        R.layout.listitem,

                        todoItems);

        lstTodo.setAdapter(aa);

        

        txtTarea.setOnKeyListener(new OnKeyListener () {

                public boolean onKey(View v, int keyCode, KeyEvent event) {

                        if (event.getAction() == KeyEvent.ACTION_DOWN

                                        && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                        		Date fecha = new Date();
                        		SimpleDateFormat formato = new SimpleDateFormat("yyyy/MM/dd");
                                todoItems.add(txtTarea.getText().toString()+"@"+formato.format(fecha));

                                aa.notifyDataSetChanged();

                                txtTarea.setText("");
                                txtTarea.setVisibility(View.GONE);
                                return true;

                        }

                        return false;

                }

        });

    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);         
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.todo_menu, menu);
            return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
            	case (R.id.add):
            		findViewById(R.id.txtTarea).setVisibility(View.VISIBLE);
            		break;
            }
            return true;
    }
    
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, v, menuInfo);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_contextual, menu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	super.onContextItemSelected(item);
    	switch (item.getItemId()) {
    		case (R.id.remove):
    			AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    			todoItems.remove(info.position);
    			aa.notifyDataSetChanged();
    			return true;
    		case (R.id.cancel):
    			return true;
    	}
    	return false;
    }
}